﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Device = SlimDX.Direct3D11.Device;
using Buffer = SlimDX.Direct3D11.Buffer;
using KinectBender.Shaders;
using SlimDX;
using System.Diagnostics;

namespace KinectBender
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        IPhysicsScene scene;

        public MainWindow()
        {
            InitializeComponent();

            scene = new FluidPhysicsScene(600, 600);
            this.image.Source = scene.ImageSource;
            scene.FPSUpdated += new EventHandler(scene_FPSUpdated);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);

            //TestGPU();
            this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
        }

        void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                scene.IsFountainOn = !scene.IsFountainOn;
            }
        }

        void scene_FPSUpdated(object sender, EventArgs e)
        {
            txtFPS.Text = scene.FPS.ToString();
            txtCount.Text = scene.NumParticles.ToString();
        }

        string SHADER = "KinectBender.Shaders.IntegrateParticles.hlsl";

        private void TestGPU()
        {
            /*
            Device device = new Device(SlimDX.Direct3D11.DriverType.Hardware, 
                                       SlimDX.Direct3D11.DeviceCreationFlags.Debug,
                                       SlimDX.Direct3D11.FeatureLevel.Level_10_0);
            var integrate = ShaderHelper.LoadComputeShader(device, SHADER, "Integrate");

            // Create GPUList of particles using the immediate context
            GPUList<GPUParticle> particles = new GPUList<GPUParticle>(device.ImmediateContext);

            // Add some particles
            for (int i = 0; i < 10; i++)
            {
                particles.Add(new GPUParticle() { Position = new Vector3(i, 0, 0), Velocity = new Vector3(0, i, -i) });
            }

            // Print the particles            

            Debug.WriteLine("Before Integration:"); 
            
            foreach (GPUParticle p in particles) 
                Debug.WriteLine(p.ToString());            
            
            // Run the compute shader            
            device.ImmediateContext.ComputeShader.Set(integrate);            
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(particles.UnorderedAccess, 0);
            device.ImmediateContext.Dispatch(particles.Count, 1, 1);            
            
            // Print the particles again            
            Debug.WriteLine("\n\nAfter Integration:");            
            foreach (GPUParticle p in particles)                
                Debug.WriteLine(p.ToString());
            */
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            scene.Start();
        }
    }
}
